﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace libLite
{
    public partial class LogOn : Form
    {
        public static IDbConnection ConObj;
        public IDbCommand dbcmd;
        public static libLite.Korisnik logUser;

        public LogOn()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string connectionString = "URI=file:baza.db";
            logUser = new Korisnik();

            ConObj = (IDbConnection)new SQLiteConnection(connectionString);
            ConObj.Open();
            dbcmd = ConObj.CreateCommand();
            dbcmd.CommandText = "SELECT ime,prezime,fk_tip,username FROM korisnici where username='"+user.Text+"' and password='"+pass.Text+"'";
            IDataReader reader = dbcmd.ExecuteReader();

            if (reader.Read())
            {
                logUser.sIme(reader.GetString(0));
                logUser.sPrezime(reader.GetString(1));
                logUser.sPristup(reader.GetInt32(2).ToString());
                logUser.sUser(reader.GetString(3));
                this.Hide();
                (new MainForm()).Show();
            }
            else MessageBox.Show("Pogresan login");

            reader.Close();
            reader = null;
            dbcmd.Dispose();
            dbcmd = null;
        }

        private void pass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
                button3_Click(this, e);
        }
    }
}
